#!/bin/bash

load_config() {
  # @TOREMOVE
  # Fix Old ZMICRORC
  local old_home=$(config::get HOME)
  if [ "$old_home" != "" ]; then
    log::debug "remove old config .zmicrorc"

    config::remove HOME
    config::remove BIN
    config::remove VERSION
  fi

  CONFIG_PATH=$1
  if [ "$CONFIG_PATH" = "" ]; then
    log "load_config: Config Path is required"
    exit 1
  fi

  if [ ! -f $CONFIG_PATH ] && [ ! -d $CONFIG_PATH ]; then
    log "load_config: Config Path(${CONFIG_PATH}) not found"
    exit 1
  fi

  # config::load_core_mod

  if [ -f $CONFIG_PATH ]; then
    config::load_file $CONFIG_PATH
  elif [ -d $CONFIG_PATH ]; then
    config::load_dir $CONFIG_PATH
  else
    log "load_config: Invalid Config Path(${CONFIG_PATH})"
    exit 1
  fi
}

config::load_file() {
  local CONFIG_FILE=$1

  if [ -z "$CONFIG_FILE" ]; then
    log::error "[config::load_file] config file path is required"
    exit 1
  fi

  if [ -f $CONFIG_FILE ]; then
    log::debug "[$(timestamp)][config::load_file]: $CONFIG_FILE"

    # \. $CONFIG_FILE
    . $CONFIG_FILE
  fi
}

config::load_dir() {
  CONFIG_DIR=$1
  CONFIG_FILES=$(ls $CONFIG_DIR)

  for CONFIG_FILE in $CONFIG_FILES; do
    # echo "load config: $CONFIG_FILE"

    config::load_file $CONFIG_DIR/$CONFIG_FILE
  done
}

config::load_mod() {
  if [ -z $VERSION ]; then
    log::error "VERSION is required"
    exit 1
  fi

  export ZMICRO_VERSION=$VERSION
  export ZMICRO_DESCRIPTION=$DESCRIPTION

  # if [ -n $DEPENDENCIES ]; then
  #   echo "load mod config found depdencies: $DEPENDENCIES"
  #   array::each require $DEPENDENCIES
  # fi
}

config::load_core_mod() {
  config::load_file $ZMICRO_MOD

  config::load_mod
}

config::load_user_config() {
  [[ -f $ZMICRO_RC ]] && load_config $ZMICRO_RC
}

config::get() {
  # If not found, return empty string
  if [ ! -f $ZMICRO_RC ]; then
    echo ""
    return
  fi

  local key=$1
  local value=$(cat $ZMICRO_RC | grep "${key}=" | awk -F '=' '{print $2}')
  echo $value
}

config::set() {
  local key=$1
  local value=$2
  local filepath=$3

  if [ -z "$filepath" ]; then
    filepath=$ZMICRO_RC
  elif [ ! -f "$filepath" ]; then
    log::error "[config::set] file(${filepath}) not found"
    exit 1
  fi

  local line=""
  # If not found, return empty string
  if [ -f $filepath ]; then
    line=$(cat $filepath | grep "${key}=")
  fi

  # New Key
  if [ "$line" = "" ]; then
    echo "${key}=${value}" >>$filepath
    return
  fi

  # Old Key
  local newLine="${key}=${value}"
  echo $newLine
  sed -i -e "s%${line}%${newLine}%g" $ZMICRO_RC
}

config::remove() {
  local key=$1
  local line=$(cat $ZMICRO_RC | grep "${key}=")

  # New Key
  if [ "$line" != "" ]; then
    sed -i -e "s%${line}%%g" $ZMICRO_RC
    sed -i "/^\s*$/d" $ZMICRO_RC
  fi
}

config::list() {
  less $ZMICRO_RC
}

export -f config::load_file
export -f config::load_dir

export -f load_config

export -f config::load_mod
export -f config::load_core_mod
export -f config::load_user_config

export -f config::list
export -f config::get
export -f config::set
export -f config::remove
